package com.bjfu.gmms.servlet.adminSecretaryServlet;

import com.bjfu.gmms.MyBatisUtil;
import com.bjfu.gmms.mapper.AdminSecretaryMapper;
import com.bjfu.gmms.model.Pairing;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/allResultsQueryServlet")
public class AllResultsQueryServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        int i_collegeId = Integer.parseInt(session.getAttribute("collegeId").toString());
        String collegeId = String.format("%03d", i_collegeId);
        session.setAttribute("role", "as");
        try(SqlSession sqlSession = MyBatisUtil.getSqlSession("development")){
            AdminSecretaryMapper mapper = sqlSession.getMapper(AdminSecretaryMapper.class);
            // 调用 Mapper 中的方法查询数据
            List<Pairing> pairingList = mapper.getCollegeMentorStudentPairing(collegeId);
            request.getSession().setAttribute("pairingList", pairingList);
            request.getRequestDispatcher("allResultsQuery.jsp").forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            // 处理异常并跳转到错误页面
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "查询失败");
        }
    }
}
